package top.jnaw.jee.utils;

import org.fusesource.jansi.Ansi;
import org.fusesource.jansi.Ansi.Color;
import org.fusesource.jansi.AnsiConsole;

/**
 * Created by neo on 17-6-1.
 *
 * 好看的终端日志工具类
 */
public class Log {

  private static String HEADER = null;

  /**
   * 初始化，IDEA 必须先调用这个
   */
  public static void init() {
    init(null);
  }

  /**
   * 初始化，IDEA 必须先调用这个，可重复调用修改前缀
   *
   * @param title 每次打印日志的前缀
   */
  public static void init(String title) {
    System.setProperty("jansi.passthrough", "true");
    AnsiConsole.systemInstall();

    HEADER = (null == title) ? " => " :
             Ansi.ansi().fg(Color.WHITE).a(title + " => ").reset().toString();
  }

  /**
   * 销毁初始化的设置
   */
  public static void deinit() {
    System.setProperty("jansi.passthrough", "false");
    AnsiConsole.systemUninstall();
  }

  private static Ansi genAnsi(Color fg, String msg) {
    return Ansi.ansi().fg(fg).a(msg).reset();
  }

  /**
   * Debug log
   */
  public static void d(String msg) {
//    Logger.getRootLogger().debug(HEADER + genAnsi(Color.BLUE, msg));
    System.out.println(HEADER + genAnsi(Color.BLUE, msg));
  }

  /**
   * Info log
   */
  public static void i(String msg) {
//    Logger.getRootLogger().info(HEADER + genAnsi(Color.GREEN, msg));
    System.out.println(HEADER + genAnsi(Color.GREEN, msg));
  }

  /**
   * Warning log
   */
  public static void w(String msg) {
//    Logger.getRootLogger().warn(HEADER + genAnsi(Color.YELLOW, msg));
    System.out.println(HEADER + genAnsi(Color.YELLOW, msg));
  }

  /**
   * Error log
   */
  public static void e(String msg) {
//    Logger.getRootLogger().error(HEADER + genAnsi(Color.RED, msg));
    System.out.println(HEADER + genAnsi(Color.RED, msg));
  }
}
